//
//  ViewController.m
//  03_图层几何学
//
//  Created by apple on 16/5/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *fenzhen;
@property (weak, nonatomic) IBOutlet UIImageView *miaozhen;
@property (weak, nonatomic) IBOutlet UIImageView *biaopan;


@property (weak, nonatomic) IBOutlet UIView *LayerView;
@property (weak, nonatomic) IBOutlet UIView *LayerView2;

@property(nonatomic,retain) CALayer *blueLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    /**
     * frame 属性 是根据 , bounds , position 和 transform 计算而来
     *
     * center 和 position 都代表了相对于父视图的 anchorPoint(锚点)所在的位置
     *
     * anchorPoint (默{0.5,0.5})
     * 改变 anchorPoint属性 , position(中心点)属性不变 frame属性将被改变.
     *
     */

    [self clockDemo];

    [self getLayerPointAndRect];
    
    [self geometryFlippedAttribute];
    
    [self zPositionAttribute];
    
    [self LayerOfTouch];


}

//anchorPoint属性
- (void)clockDemo{

    
    self.fenzhen.layer.anchorPoint = CGPointMake(0.5, 0.9);
    self.miaozhen.layer.anchorPoint = CGPointMake(0.5, 0.9);
    
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
    



}

- (void)changeTime{

    NSDate *date = [NSDate date];
    
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    NSDateComponents *component = [calender components:NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:date];

    CGFloat fenzhen = (component.minute / 60.0) * M_PI * 2.0;
    
    CGFloat miaozhen = (component.second / 60.0) * M_PI *2.0;
    
    self.fenzhen.transform = CGAffineTransformMakeRotation(fenzhen);
    self.miaozhen.transform = CGAffineTransformMakeRotation(miaozhen);


}


- (void)getLayerPointAndRect{


    CGPoint Pfenzhen = [self.fenzhen.layer convertPoint:self.biaopan.bounds.origin fromLayer:self.biaopan.layer];

    NSLog(@"%f,%f",Pfenzhen.x,Pfenzhen.y);
    

    
    /** 可以把定义在一个图层坐标系下的点或者矩形转换成另一个图层坐标系下的点或者矩形
     *??????
     - (CGPoint)convertPoint:(CGPoint)point fromLayer:(CALayer *)layer;
     - (CGPoint)convertPoint:(CGPoint)point toLayer:(CALayer *)layer;
     - (CGRect)convertRect:(CGRect)rect fromLayer:(CALayer *)layer;
     - (CGRect)convertRect:(CGRect)rect toLayer:(CALayer *)layer;
     */
    
    

}

//geometryFlipped 属性
- (void)geometryFlippedAttribute{

    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    view.backgroundColor = [UIColor redColor];
    
    //改变坐标原点 , YES的话 , 坐标原点将变为左下角.
    self.LayerView.layer.geometryFlipped = YES;
    
    [self.LayerView addSubview:view];
    

}



//zPosition 属性
- (void)zPositionAttribute{

    /**
     * zPosition 和 anchorPointZ 都是描素Z轴的属性.
     * 改变 zPosition的值 , 可以改变图层显示顺序.
     * 默认为 0.0f
     */
    
    NSLog(@"LayerView : %lf",self.LayerView.layer.zPosition);
    NSLog(@"LayerView2 : %lf",self.LayerView2.layer.zPosition);
    
    self.LayerView.layer.zPosition = 1;

}

//containsPoint 和 hitTest 属性
//独立的图层内 响应事件.
- (void)LayerOfTouch{

    //containsPoint 属性(BOOL类型)
    //可以判断 一个视图上 , 判断哪个图层被点击

    self.blueLayer = [CALayer layer];
    self.blueLayer.frame = CGRectMake(0, 0, 50, 50);
    self.blueLayer.backgroundColor = [UIColor blueColor].CGColor;

    [self.LayerView2.layer addSublayer:self.blueLayer];

    //hitTest 属性(返回 CALayer类型)
    //返回 被点击的layer层 对象.
    
    /** 注意!!!!
     *
     * hitTest属性根据事件传递顺序 来获取对象
     * zPosition 可以改变图层顺序 , 但是不能改变事件传递顺序.
     *
     */
    
    

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    CGPoint point = [[touches anyObject] locationInView:self.LayerView2];

    NSLog(@"%lf,%lf",point.x,point.y);
    if ([self.blueLayer containsPoint:point]) {
        
        NSLog(@"self.blueLayer");
        
    }else{
    
        NSLog(@"self.LayerView2");
    }
    



}




@end
